package org.arl.modem.codec;

/*
 * Description: Generates chaotic time series. Input values: numOfPackets-
 * number of source messages (k) k- initial value for the random number
 * generator packetpos- The sequence number of the packet to be encoded. m- The
 * key value for the kent map. Returns : The value of x(packetpos).(ie) The
 * timeseries value at the position packetpos.
 */
public class KentMap {
   float[] x;

   public float kentMap(int numOfPackets, float k, int packetpos, float m) {
      int index = 0;

      x = new float[numOfPackets];

      for (index = 0; index < numOfPackets; index++) {
         if (index == 0) {
            x[0] = k;
         } else {
            if (x[index - 1] <= m) {
               x[index] = (float) (x[index - 1] / m);
            } else {
               x[index] = (float) ((1 - x[index - 1]) / (1 - m));
            }
         }
         // System.out.println("x " + x[index]);
      }
      return (float) x[packetpos];
   }

   /*
    * Description: Finds the position of the largest value in the time series.
    * Input values: numOfPackets- Number of source messages (k) Returns: The
    * position of the largest value in the series
    */
   public int kentMap1(int numOfPackets) {

      int index = 0;
      int index1 = 0;
      int index3 = 0;

      for (index = 0; index < numOfPackets; index++) {
         index3 = 0;
         for (index1 = 0; index1 < numOfPackets; index1++) {

            if (x[index] >= x[index1]) {
               index3++;
            } else {
               break;
            }
         }
         // System.out.println("index " + index +" index3 " + index3);
         if (index3 == numOfPackets) {
            break;
         }
      }
      int pos = index;
      x[index] = 0;
      // System.out.println("x index" + x[index]);
      return pos;
   }
}
